package com.lrkj.workorder.controller;

import com.lrkj.common.core.domain.entity.SysUser;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.workorder.domain.WorkOrderLog;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.Api;
import java.util.List;
import java.util.Map;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lrkj.common.annotation.Log;
import com.lrkj.common.core.controller.BaseController;
import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.enums.BusinessType;
import com.lrkj.workorder.domain.WorkOrder;
import com.lrkj.workorder.service.IWorkOrderService;
import com.lrkj.common.utils.poi.ExcelUtil;
import com.lrkj.common.core.page.TableDataInfo;

/**
 * 工单管理Controller
 *
 * @author yajun_ren
 * @date 2022-04-21
 */
@RestController
@RequestMapping("/workorder/workOrder")
@Api(tags = "工单管理")
public class WorkOrderController extends BaseController
{
    @Autowired
    private IWorkOrderService workOrderService;

    /**
     * 查询工单管理列表
     */
    @ApiOperation(value = "查询工单管理列表", notes = "查询工单管理列表")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrder workOrder)
    {
        startPage();
        this.changeParameter(workOrder.getParams());
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        return getDataTable(list);
    }

    /**
     * 导出工单管理列表
     */
	@ApiOperation(value = "导出工单管理列表", notes = "导出工单管理列表")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:export')")
    @Log(title = "工单管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WorkOrder workOrder)
    {
        List<WorkOrder> list = workOrderService.selectWorkOrderList(workOrder);
        ExcelUtil<WorkOrder> util = new ExcelUtil<WorkOrder>(WorkOrder.class);
        return util.exportExcel(list, "工单管理数据");
    }

    /**
     * 获取工单管理详细信息
     */
	@ApiOperation(value = "获取工单管理详细信息", notes = "获取工单管理详细信息")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(workOrderService.selectWorkOrderById(id));
    }

    /**
     * 新增工单管理
     */
    @ApiOperation(value = "新增工单管理", notes = "新增工单管理")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:add')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WorkOrder workOrder)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        workOrder.setCharterMemberId(user.getUserId() + "");
        workOrder.setCharterMemberName(user.getNickName());
        return toAjax(workOrderService.insertWorkOrder(workOrder));
    }

    /**
     * 修改工单管理
     */
    @ApiOperation(value = "修改工单管理", notes = "修改工单管理")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:edit')")
    @Log(title = "工单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.updateWorkOrder(workOrder));
    }

    /**
     * 删除工单管理
     */
    @ApiOperation(value = "删除工单管理", notes = "删除工单管理")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:remove')")
    @Log(title = "工单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(workOrderService.deleteWorkOrderByIds(ids));
    }



    /**
     * 派单
     */
    @ApiOperation(value = "派单", notes = "派单")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:sendOrders')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/sendOrders")
    public AjaxResult sendOrders (@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.sendOrders(workOrder));
    }

    /**
     * 催办
     */
    @ApiOperation(value = "催办", notes = "催办")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:urgeOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/urgeOrder")
    public AjaxResult urgeOrder (@RequestBody WorkOrder workOrder)
    {
        return toAjax(workOrderService.urgeOrder(workOrder));
    }


    /**
     * 办结
     */
    @ApiOperation(value = "办结", notes = "办结")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:conCludeOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/conCludeOrder")
    public AjaxResult conCludeOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.conCludeOrder(workOrder));
    }


    /**
     * 回访
     */
    @ApiOperation(value = "回访", notes = "回访")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:returnOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/returnOrder")
    public AjaxResult returnOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.returnOrder(workOrder));
    }


    /**
     * 转交
     */
    @ApiOperation(value = "转交", notes = "转交")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:careOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/careOrder")
    public AjaxResult careOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.careOrder(workOrder));
    }



    /**
     * 取消
     */
    @ApiOperation(value = "取消", notes = "取消")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:chargebackOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/chargebackOrder")
    public AjaxResult chargebackOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.chargebackOrder(workOrder));
    }


    /**
     * 挂单
     */
    @ApiOperation(value = "挂单", notes = "挂单")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:suspendOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/suspendOrder")
    public AjaxResult suspendOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.suspendOrder(workOrder));
    }


    /**
     * 取消挂单
     */
    @ApiOperation(value = "取消挂单", notes = "取消挂单")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:endSuspendOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/endSuspendOrder")
    public AjaxResult endSuspendOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.endSuspendOrder(workOrder));
    }


    /**
     * 返修
     */
    @ApiOperation(value = "返修", notes = "返修")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:repairOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/repairOrder")
    public AjaxResult repairOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.repairOrder(workOrder));
    }


    /**
     * 退单
     */
    @ApiOperation(value = "退单", notes = "退单")
    @PreAuthorize("@ss.hasPermi('workorder:workOrder:retreatOrder')")
    @Log(title = "工单管理", businessType = BusinessType.INSERT)
    @PostMapping("/retreatOrder")
    public AjaxResult retreatOrder(@RequestBody WorkOrder workOrder){
        return toAjax(workOrderService.retreatOrder(workOrder));
    }


    /**
     * 根据状态统计数量
     * @return
     */
    @ApiOperation(value = "根据状态统计数量", notes = "根据状态统计数量")
    @GetMapping("/selCountByStatus")
    public TableDataInfo selCountByStatus(){
        return  getDataTable(workOrderService.selCountByStatus());
    }
}
